package com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.group;

import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.webmanager.business.maintain_performance.constant.ConditionLogic;
import com.tbit.uqbike.webmanager.business.maintain_performance.constant.PerformanceBizType;
import com.tbit.uqbike.webmanager.business.maintain_performance.constant.PerformanceRuleType;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.PerformanceRule;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.PerformanceRuleCondition;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.PerformanceRuleGroup;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.Range;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.BizRuleGroup;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.rule.LowPowerRule;
import lombok.Data;

import java.util.*;

/**
 * 低电优先换电配置
 *
 * @author <a href="mailto:zheng.zebiao@tbit.com.cn">zb</a>
 * @since 2025/2/19
 */
@Data
public class LowPowerBasedGroup implements BizRuleGroup {
    /**
     * 是否启用
     */
    private Boolean enabled;
    /**
     * 低电绩效规则
     */
    private List<LowPowerRule> subRules;

    @Override
    public PerformanceRuleGroup toRaw(PerformanceBizType bizType, Integer accountId) {
        PerformanceRuleGroup group = new PerformanceRuleGroup();
        group.setAccountId(accountId);
        group.setBizType(bizType);
        group.setRuleType(PerformanceRuleType.LOW_POWER);
        group.setEnabled(enabled);
        group.setRules(new ArrayList<>());

        if (subRules == null) {
            return group;
        }
        for (LowPowerRule rule : subRules) {
            PerformanceRule performanceRule = new PerformanceRule();
            if (rule.getPerformance() == null) {
                throw new BaseException("低电优先绩效-绩效值不能为空");
            }
            performanceRule.setPerformance(rule.getPerformance());
            performanceRule.setConditionLogic(ConditionLogic.AND);
            performanceRule.setConditions(new ArrayList<>());

            // 换电前电量
            if (rule.getBeforeChangePower() == null) {
                throw new BaseException("低电优先绩效-换电前电量区间不能为空");
            }
            performanceRule.getConditions().add(rule.getBeforeChangePower().toCondition("beforeChangePower"));

            // 优先级
            if (rule.getPriority() == null) {
                throw new BaseException("低电优先绩效-优先级不能为空");
            }
            Map<String, Object> metaInfo = new HashMap<>();
            metaInfo.put("priority", rule.getPriority());
            performanceRule.setMetaInfo(metaInfo);

            group.getRules().add(performanceRule);
        }
        return group;
    }

    @Override
    public void initFromRaw(PerformanceRuleGroup group) {
        setEnabled(group.getEnabled());
        subRules = new ArrayList<>();
        for (PerformanceRule rule : group.getRules()) {
            LowPowerRule lowPowerRule = new LowPowerRule();
            for (PerformanceRuleCondition condition : rule.getConditions()) {
                if ("beforeChangePower".equals(condition.getKey())) {
                    lowPowerRule.setBeforeChangePower(Range.from(condition, Integer::parseInt));
                }
            }
            if (rule.getMetaInfo() != null) {
                lowPowerRule.setPriority((Integer) rule.getMetaInfo().get("priority"));
            }
            lowPowerRule.setPerformance(rule.getPerformance());
            subRules.add(lowPowerRule);
        }
    }

    @Override
    public void initDefault() {
        enabled = false;
        subRules = Collections.emptyList();
    }
}
